package com.handinglian.ai.common.component;

import com.handinglian.ai.common.dto.ResultData;
import com.handinglian.ai.common.exception.BizException;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author pxl
 * @description
 * @date 2019/4/12 16:27
 */
@Slf4j
@RestControllerAdvice
public class ExceptionAdvice {
    @ExceptionHandler(value = {Exception.class
    })
    public ResultData handleException(
            Throwable throwable) {
        log.error("异常处理",throwable);
        return ResultData.failed();
    }

    @ExceptionHandler(value = {UnauthorizedException.class
    })
    public ResultData handleUnauthorizedException(
            Throwable throwable) {
        log.error("验证异常处理",throwable);
        return ResultData.authorize();
    }

    @ExceptionHandler(value = {DuplicateKeyException.class
    })
    public ResultData handleDuplicateKeyException(
            Throwable throwable) {
        log.error("重复异常处理",throwable);
        return ResultData.duplicate();
    }

    @ExceptionHandler(value = {BizException.class
    })
    public ResultData handleBizException(
            Throwable throwable) {
        log.error("业务异常处理",throwable);
        BizException bizException = (BizException) throwable;
        ResultData resultData = new ResultData(bizException.getCode(), bizException.getMsg());
        return resultData;
    }
}
